package com.neo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neo.dao.CheckGroupDao;
import com.neo.entity.PageResult;
import com.neo.entity.QueryPageBean;
import com.neo.pojo.CheckGroup;
import com.neo.pojo.CheckItem;
import com.neo.service.CheckGroupService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        //设置中间表
        addCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }

    private void addCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        Map<String, Integer> map = null;
        if(ArrayUtils.isNotEmpty(checkitemIds)){
            for (Integer checkItemId : checkitemIds) {
                map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkItemId);
                checkGroupDao.addCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean){
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id){
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        return checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
    }

    @Transactional
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds){
        //更新检查组
        checkGroupDao.update(checkGroup);
        //先删除关联
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroup.getId());
        //重新建立关联
        addCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }

    @Transactional
    @Override
    public void delete(Integer checkGroupId){
        //删除关联
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroupId);
        //删除检查组
        checkGroupDao.deleteById(checkGroupId);
    }

    @Override
    public List<CheckGroup> findAll(){
        return checkGroupDao.findAll();
    }
}
